/*
 * Copyright 2010 Thedwick, LLC

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

 */
package com.thedwick.jdbcGrabber.sink;

public class TraceElement {
	private final StackTraceElement[] stackTrace;
	private final String sql;
	private final Integer traceKey;
	
	public TraceElement(StackTraceElement[] stackTrace, String sql){
		this.stackTrace = stackTrace;
		this.sql = sql;
		this.traceKey = calculateTraceKey();
	}

	public StackTraceElement[] getStackTrace() {
		return stackTrace;
	}

	public String getSql() {
		return sql;
	}

	public Integer calculateTraceKey() {
		StringBuilder builder = new StringBuilder();
		for (StackTraceElement element : this.stackTrace){
			builder.append(element.toString());
			builder.append("\n");
		}
		/*
		 * I realize this is not necessarily unique to the string, but my gamble here is that it's unique enough
		 * for our purposes and it's also very fast.  If I turn out to be wrong, then at least it's easy to fix
		 * just in this one place.
		 */
		return new Integer(builder.toString().hashCode());
	}
}